import proFetch from '../../../../hrpub/common/utils/project-fetch';
import {promptBox,toast} from 'nc-lightapp-front';

export default class BtnAction{
    constructor(comp){
        this.comp = comp

    }



    onButtonClick =(props, btn)=>{
        const {action} = this.comp
        const {main} = props
        switch(btn){
            case 'add':
                props.dispatch({
                    type: 'main/add',
                    payload: {
                        type: main.type,
                        pk_org: main.orgvalue.refpk
                    }
                }).then((res)=>{
                    if(res.data && res.data.handitemsetgrid){
                        props.editTable.setTableData('handitemsetgrid',res.data.handitemsetgrid)
                    }else{
                        props.editTable.setTableData('handitemsetgrid',{rows:[]}) 
                    }
                    props.form.EmptyAllFormValue('handcondform')
                    props.form.setFormStatus('handcondform','edit')
                    props.editTable.setStatus('handitemsetgrid','edit')
                   
                    props.button.setButtonVisible({
                        'edit' : false,
                        'save': true,
                        'cancel': true
                    })
                    props.dispatch({
                        type: 'main/update',
                        payload: {
                            showPage: 'card',
                            cardstatus: 'edit',
                            operation: 'add'
                        }
                    })
                })
                break;
            case 'delete':
                let deldata = props.editTable.getAllRows('handcondgrid')
                let pkStr = []
                if(deldata.length>0){
                    deldata.forEach((item) => {
                        if(item.selected){
                            pkStr.push(item.values.pk_handcond.value)  
                        }
                    });
                }
                promptBox(
                    {
                        color: "warning",
                        title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                        content: main.json['gx6008-000019'], /* 国际化处理： 确定删除所选数据*/
                        beSureBtnClick: function () {
                            props.dispatch({
                                type: 'main/delTableData',
                                payload: {
                                    pk_handconds: pkStr.join()
                                }
                            }).then((res)=>{
                                action.pubAction.getTableData(main.orgvalue,main.type,main.pageInfo)
                                props.button.setButtonDisabled({
                                    'delete': true
                                })
                            })
                        }
                    })
               
                break;
            case 'edit':
                props.editTable.setStatus('handitemsetgrid','edit')
                props.button.setButtonVisible({
                    'save': true,
                    'cancel': true,
                    'edit': false
                })
                props.dispatch({
                    type: 'main/update',
                    payload: {
                        cardstatus: 'edit',
                        operation: 'edit'
                    }
                })
                break;
            case 'save':
                let formData = props.form.getAllFormValue('handcondform')
                let gridData = props.editTable.getAllRows('handitemsetgrid')
                let data ={
                    type: main.type,
                    pk_org: main.orgvalue.refpk,
                    handcondform: formData,
                    handitemsetgrid: {rows:gridData}
                }
                proFetch({
                    url: '/nccloud/hrtrn/handcond/HandCondSaveAction.do',
                    body: data
                }).then((res)=>{
                    if(res.data){
                        props.form.setFormItemsValue('handcondform',{'pk_handcond':{value: res.data.pk_handcond}})
                    }
                    props.form.setFormStatus('handcondform','browse')
                    props.editTable.setStatus('handitemsetgrid','browse')
                    props.button.setButtonVisible({
                        'edit' : true,
                        'save': false,
                        'cancel': false
                    })
                    props.dispatch({
                        type: 'main/update',
                        payload: {
                            showPage: 'card',
                            cardstatus: 'browse',
                        }
                    })
                })
                break;
            case 'cancel':
                let pk_handcond = props.form.getFormItemsValue('handcondform','pk_handcond').value
                promptBox(
                    {
                        color: "warning",
                        title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                        content: main.json['gx6008-000037'], /* 国际化处理： 是否确认要取消*/
                        beSureBtnClick: function () {
                            props.editTable.setStatus('handitemsetgrid','browse')
                            props.form.setFormStatus('handcondform','browse')
                            if(main.operation!=='add'){
                                action.pubAction.showFormPage('browse',pk_handcond)
                            }
                            props.dispatch({
                                type: 'main/update',
                                payload: {
                                    showPage: main.operation==='add'?'list':'card',
                                    cardstatus: 'browse'
                                }
                            })
                            props.button.setButtonVisible({
                                'edit': true,
                                'save': false,
                                'cancel': false
                            })
                        }
                    })
                break;
            default :
                break;
        }

    }

    //返回事件
    onBack =()=>{
        const{props,action} = this.comp
        const{main} = props
        if(main.cardstatus === 'edit'){
            promptBox(
                {
                    color: "warning",
                    title: main.json['gx6008-000018'], /* 国际化处理： 提示*/
                    content: main.json['gx6008-000074'], /* 国际化处理： 是否确认要返回*/
                    beSureBtnClick: function () {
                        props.button.setButtonVisible({
                            'edit': true,
                            'save': false,
                            'cancel': false
                        })
                        props.dispatch({
                            type: 'main/update',
                            payload: {
                                showPage: 'list',
                                cardstatus: 'browse',
                                operation: 'browse'
                            }
                        })
                    }
                })
        }else{
            props.dispatch({
                type: 'main/update',
                payload: {
                    showPage: 'list',
                    operation: 'browse',
                    cardstatus: 'browse',
                }
            })
        }
        props.button.setButtonDisabled({
            'delete': true
        })
        props.form.setFormStatus('handcondform','browse')
        action.pubAction.getTableData(main.orgvalue,main.type,main.pageInfo)
    }

     //每页显示条数
     pageSizeSelect = (val) => {
        const {props,action} = this.comp
        const {main} = props
        main.pageInfo.pageSize = val;
        main.pageInfo.pageIndex = 0;
        props.dispatch({
            type: 'main/update',
            payload: {
                pageInfo: main.pageInfo
            }
        })
        action.pubAction.getTableData(main.orgvalue,main.type,main.pageInfo)
        
    }

    //分页事件
    paginationEve = (key) => {
        const {props,action} = this.comp
        const {main} = props
        main.pageInfo.pageIndex = key;
        props.dispatch({
            type: 'main/update',
            payload: {
                pageInfo: main.pageInfo
            }
        })
        action.pubAction.getTableData(main.orgvalue,main.type,main.pageInfo)
    };
}